package com.tarena.lbs.common.principle;

import java.io.Serializable;

import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.common.enums.Roles;
import com.tarena.lbs.common.utils.AuthenticationContextUtils;
import lombok.AccessLevel;
import lombok.Data;
import lombok.experimental.FieldDefaults;

@Data
@FieldDefaults(level = AccessLevel.PRIVATE)
public class UserPrinciple implements Serializable {
    Integer id;
    String nickname;
    Roles role;

    public static UserPrinciple getCurrentUser() throws BusinessException {
        UserPrinciple userPrinciple = AuthenticationContextUtils.get();
        if (userPrinciple == null) {
            throw new BusinessException("-1", "用户未登录或登录状态已失效");
        }
        return userPrinciple;
    }
}
